package com.jftone.bh.app.service;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.model.NtbUser;
import com.jftone.bh.app.model.SysArea;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Service;
import org.jftone.dao.Dao;
import org.jftone.exception.DaoException;
import org.jftone.util.DataMap;
import org.jftone.util.IData;
import org.jftone.util.Page;
import org.jftone.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

@Service
public class AreaService {

	
	@Autowired
	private Dao dao;

	public List<SysArea> loadAreaList(Page page) throws DaoException {
		IData<String, Object> param = new DataMap<String, Object>();
		param.put("parentId",0);
		List<SysArea> list = dao.query(SysArea.class,param,page);
		return list;
	}

    public SysArea getSysArea(IData<String, Object> param) throws DaoException {
		return dao.get(SysArea.class,param);
    }

	public int saveSysArea(IData<String, Object> data) throws DaoException {
		SysArea area = new SysArea();
		area.setMaxVip(data.getInt("maxVip",0));
		IData<String,Object> param = new DataMap<String,Object>();
		area.setId(data.getInt("areaId"));
		param.put("uplevel", SysConst.VIP_SUB_SUB);
		param.put("affiliation",area.getId());
		int havaVip = (int)dao.count(NtbUser.class,param);
		if(area.getMaxVip()<havaVip){
			return 1;
		}
		area.setResidueVip(area.getMaxVip() - havaVip);
		dao.update(area);
		return 0;
	}
}
